<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/20 16:14
 * Modified Time: 2019/7/20 16:14
 * Version: 2.0
 */

namespace App\Modules\MemberCard\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;

class GetCardTypeService extends BaseCriterias
{
    private $card_type_id;

    public function __construct($card_type_id)
    {
        $this->card_type_id = $card_type_id;
    }

    public function apply($model, RepositoryInterface $repository)
    {

        $card_type_id = $this->card_type_id;

        $model = $model->selectRaw('id')
            ->with(['cardTypeService' => function ($query) {
                $select = [
                    'name',
                    'discount',
                    'type_id',
                    'price'
                ];
                $select = implode($select,',');

                $query->selectRaw($select)
                    ->leftJoin('store_service', 'card_type_service.service_id', 'store_service.id');
            }])
            ->where('id',$card_type_id)->orderBy('id','DESC')
        ;

        return $model;
    }
}